package com.qst.controller;

import com.qst.util.OrderNoGenerator;
import com.qst.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产环境建议删除或禁用此控制器
 */
@RestController
@RequestMapping("/api/test/order-no")
public class OrderNoTestController {

    @Autowired
    private OrderNoGenerator orderNoGenerator;

    /**
     * 生成单个订单号
     */
    @GetMapping("/generate")
    public Result<Map<String, Object>> generateOrderNo() {
        String orderNo = orderNoGenerator.generate();
        
        Map<String, Object> data = new HashMap<>();
        data.put("orderNo", orderNo);
        data.put("date", OrderNoGenerator.parseDateFromOrderNo(orderNo));
        data.put("sequence", OrderNoGenerator.parseSequenceFromOrderNo(orderNo));
        data.put("isValid", OrderNoGenerator.isValidOrderNo(orderNo));
        data.put("todayCount", orderNoGenerator.getTodayCount());
        
        return Result.success(data);
    }

    /**
     * 批量生成订单号
     */
    @GetMapping("/generate-batch")
    public Result<Map<String, Object>> generateBatchOrderNos(
            @RequestParam(defaultValue = "10") Integer count) {
        
        if (count > 100) {
            return Result.error("单次生成数量不能超过100个");
        }
        
        List<String> orderNos = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            orderNos.add(orderNoGenerator.generate());
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("orderNos", orderNos);
        data.put("count", orderNos.size());
        data.put("todayTotal", orderNoGenerator.getTodayCount());
        
        return Result.success(data);
    }

    /**
     * 验证订单号格式
     */
    @GetMapping("/validate")
    public Result<Map<String, Object>> validateOrderNo(@RequestParam String orderNo) {
        boolean isValid = OrderNoGenerator.isValidOrderNo(orderNo);
        
        Map<String, Object> data = new HashMap<>();
        data.put("orderNo", orderNo);
        data.put("isValid", isValid);
        
        if (isValid) {
            data.put("date", OrderNoGenerator.parseDateFromOrderNo(orderNo));
            data.put("sequence", OrderNoGenerator.parseSequenceFromOrderNo(orderNo));
        } else {
            data.put("error", "订单号格式不正确");
        }
        
        return Result.success(data);
    }

    /**
     * 获取今日订单数量
     */
    @GetMapping("/today-count")
    public Result<Map<String, Object>> getTodayCount() {
        int count = orderNoGenerator.getTodayCount();
        
        Map<String, Object> data = new HashMap<>();
        data.put("date", LocalDate.now().toString());
        data.put("count", count);
        
        return Result.success(data);
    }

    /**
     * 重置今日计数器（谨慎使用）
     */
    @PostMapping("/reset-today")
    public Result<String> resetTodayCounter() {
        int oldCount = orderNoGenerator.getTodayCount();
        orderNoGenerator.resetTodayCounter();
        
        return Result.success("计数器已重置，原计数: " + oldCount, "重置成功");
    }

    /**
     * 使用指定日期生成订单号
     */
    @GetMapping("/generate-with-date")
    public Result<Map<String, Object>> generateWithDate(
            @RequestParam String date) {
        
        try {
            LocalDate localDate = LocalDate.parse(date);
            String orderNo = orderNoGenerator.generateWithDate(localDate);
            
            Map<String, Object> data = new HashMap<>();
            data.put("orderNo", orderNo);
            data.put("date", date);
            data.put("sequence", OrderNoGenerator.parseSequenceFromOrderNo(orderNo));
            
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("日期格式错误，请使用 yyyy-MM-dd 格式");
        }
    }

    /**
     * 解析订单号
     */
    @GetMapping("/parse")
    public Result<Map<String, Object>> parseOrderNo(@RequestParam String orderNo) {
        Map<String, Object> data = new HashMap<>();
        data.put("orderNo", orderNo);
        data.put("isValid", OrderNoGenerator.isValidOrderNo(orderNo));
        
        if (OrderNoGenerator.isValidOrderNo(orderNo)) {
            String dateStr = OrderNoGenerator.parseDateFromOrderNo(orderNo);
            int sequence = OrderNoGenerator.parseSequenceFromOrderNo(orderNo);
            
            data.put("prefix", orderNo.substring(0, 2));
            data.put("date", dateStr);
            data.put("sequence", sequence);
            
            // 格式化日期显示
            if (dateStr != null && dateStr.length() == 8) {
                String year = dateStr.substring(0, 4);
                String month = dateStr.substring(4, 6);
                String day = dateStr.substring(6, 8);
                data.put("formattedDate", year + "-" + month + "-" + day);
            }
        } else {
            data.put("error", "订单号格式不正确");
        }
        
        return Result.success(data);
    }

    /**
     * 获取订单号格式说明
     */
    @GetMapping("/format-info")
    public Result<Map<String, Object>> getFormatInfo() {
        Map<String, Object> info = new HashMap<>();
        info.put("format", "RC + yyyyMMdd + 序号(3位)");
        info.put("example", "RC20251001001");
        info.put("prefix", "RC (Recycle 的缩写)");
        info.put("dateFormat", "yyyyMMdd (年月日)");
        info.put("sequenceFormat", "3位数字，从001开始");
        info.put("totalLength", 13);
        
        Map<String, String> parts = new HashMap<>();
        parts.put("prefix", "RC (2位)");
        parts.put("date", "20251001 (8位)");
        parts.put("sequence", "001 (3位)");
        info.put("parts", parts);
        
        List<String> examples = new ArrayList<>();
        examples.add("RC20251001001 - 2025年10月1日的第1个订单");
        examples.add("RC20251001002 - 2025年10月1日的第2个订单");
        examples.add("RC20251231999 - 2025年12月31日的第999个订单");
        info.put("examples", examples);
        
        return Result.success(info);
    }
}

